package com.interphone.beta;

import java.net.Socket;
import org.freedesktop.gstreamer.GStreamer;
import com.interphone.beta.socket.Communicator;
import com.interphone.beta.socket.MySocket;
import com.interphone.beta.tools.MyVibrator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.graphics.drawable.shapes.Shape;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;

public class Main extends Activity{

	private String TAG = null;
	private boolean isPlaySend;
	private boolean isPlayReceive;
	private Button go;
	private Button close; 
	private Button door;
	private Communicator com;
	private static Socket soc;
	private MyVibrator myvb;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.TAG = this.getClass().toString();
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		

		setContentView(R.layout.main);

		close = (Button) this.findViewById(R.id.buttonclose);
		close.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Log.d(TAG,"close");
				com.closeComm();
				myFinishAppel();
			}
		});

		door = (Button) this.findViewById(R.id.buttonopendoor);
		door.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Log.d(TAG,"open door");
				door.setEnabled(false);
				com.openDoor();
				door.setEnabled(true);


			}
		});

		go = (Button) this.findViewById(R.id.buttongo);	
		/*go.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Log.d(TAG,"go");
				myvb.setKeep(false);
				com.start();
				go.setVisibility(View.INVISIBLE);
				door.setVisibility(View.VISIBLE);
			}
		});*/
		
		go.setOnTouchListener(new View.OnTouchListener() {
			
			@SuppressLint("NewApi")
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(event.getAction()==MotionEvent.ACTION_DOWN){
					Resources res = getResources();
					GradientDrawable sh = (GradientDrawable) res.getDrawable(R.drawable.shapegodown);
					go.setBackground(sh);
				}
				if(event.getAction()==MotionEvent.ACTION_UP){
					Log.d(TAG,"go");
					Resources res = getResources();
					GradientDrawable sh = (GradientDrawable) res.getDrawable(R.drawable.shapegoup);
					go.setBackground(sh);
					myvb.setKeep(false);
					com.start();
					go.setVisibility(View.INVISIBLE);
					door.setVisibility(View.VISIBLE);
				}
				return false;
			}
		});
		

		if(this.getIntent()!=null && this.getIntent().getExtras()!=null && this.getIntent().getExtras().getBoolean("appel") && MySocket.getSocketClient()!=null
				 && !MySocket.getSocketClient().isClosed()){
			/*initr Gstreamer*/
			try {
				System.loadLibrary("gstreamer_android");
				GStreamer.init(this);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace(); 
			}
			/*Reveil de phone meme en ecran verrouillé*/
			this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
		            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
		            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
		            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
			Log.d(TAG,"Appel !!");
			soc = MySocket.getSocketClient();
			
			/*Vibreur et sonnerie*/
			myvb = new MyVibrator((Vibrator) getSystemService(Context.VIBRATOR_SERVICE), 1500, 500);
			//myvb.start();
			
			Log.d(TAG,"Lancement du communicator");
			com = new Communicator(MySocket.getSession(), soc, this);
			/*Faire un thread qui ecoute le thread pour savoir s'il a été coupé avant la réponse du user*/
			//Log.d(TAG, "Lancement du Waiting Thread");
			//this.wt = new WaitingThread(soc, this);
			//this.wt.start();
			
			
		}else{
			initBasic();
		}

	}

	public void myFinishAppel(){
		Log.d(TAG,"myFinish");
		this.finish();
		this.onDestroy();
	}

	public void initAppel(){
		this.go.setVisibility(View.VISIBLE);
		this.close.setVisibility(View.VISIBLE);
		this.door.setVisibility(View.INVISIBLE);
	}

	public void initBasic(){
		this.go.setVisibility(View.INVISIBLE);
		this.close.setVisibility(View.INVISIBLE);
		this.door.setVisibility(View.INVISIBLE);
	}

	public boolean isPlayReceive() {
		return isPlayReceive;
	}

	public void setPlayReceive(boolean isPlayReceive) {
		this.isPlayReceive = isPlayReceive;
	}

	public boolean isPlaySend() {
		return isPlaySend;
	}

	public void setPlaySend(boolean isPlaySend) {
		this.isPlaySend = isPlaySend;
	}
	public Communicator getCom() {
		return com;
	}

	public void setCom(Communicator com) {
		this.com = com;
	}

	public Button getGo() {
		return go;
	}

	public void setGo(Button go) {
		this.go = go;
	}

	public Button getClose() {
		return close;
	}

	public void setClose(Button close) {
		this.close = close;
	}

	public Button getDoor() {
		return door;
	}

	public void setDoor(Button door) {
		this.door = door;
	}

	public void onDestroy() {
        super.onDestroy();
        android.os.Process.killProcess(android.os.Process.myPid());
    }  

}
